import axios from 'axios'; //引入axios
import store from '../store';
import router from '../router';
// 设置post请求头
// axios.defaults.headers.post["Content-Type"] =
//   "application/x-www-form-urlencoded;charset=UTF-8";
// axios.defaults.withCredentials = false; //在跨域请求时，不会携带用户凭证；返回的 response 里也会忽略 cookie

//创建axios实例，请求超时时间为300秒
const instance = axios.create({
    timeout: 300000,
    baseURL: process.env.VUE_APP_API_PATH,
});
console.log(process.env, 'ev');
//请求和响应拦截可以根据实际项目需求进行编写
// 请求发起前拦截
instance.interceptors.request.use(
    config => {
        const token = store.state.token;
        if (token != null) {
            config.headers['token'] = token; // 让每个请求携带自定义token
        }
        return config;
    },
    error => {
        // Do something with request error
        return Promise.reject(error);
    }
);

// 响应拦截（请求返回后拦截）
instance.interceptors.response.use(
    response => {
        // console.log("响应拦截", response);
        // return response.data;
        const res = response.data;
        if (res.code === 1000) {
            store.commit('UPDATE_TOAST_STATUS', true);
            store.commit('UPDATE_TOAST_TEXT', 'Page Expired');
        }
        return res;
    },
    error => {
        console.log('catch', error);
        return Promise.reject(error);
    }
);
export default instance;

